﻿using CentaLine.Infrastructure;
using ETA.Entity;
using ETA.IService;
using Ninject;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ETA.ClientService
{
    public class FileBll
    {
        private static IFileTypeService _IFileTypeService { set; get; }
        private static IUploadFileService _UploadFileService { set; get; }

        static FileBll()
        {
            IKernel kernel = KernelHelper.GetKernel();
            _IFileTypeService = kernel.Get<IFileTypeService>();
            _UploadFileService = kernel.Get<IUploadFileService>();
        }

        /// <summary>
        /// 获取文件列表
        /// </summary>
        /// <returns></returns>
        public static List<FileType> GetFileTyes()
        {
            var key = "KeyFileTypeList";
            if (CacheHelper.Get(key) != null)
            {
                return CacheHelper.Get(key) as List<FileType>;
            }

            var res = new List<FileType>();
            var response = _IFileTypeService.QueryFileType(new QueryFileTypeRequest());
            if (response.Result.IsSuccess && response.FileTypeList.Any())
            {
                res = response.FileTypeList.ToList();
                CacheHelper.AddHours(key, res, 20);
            }
            return res;
        }

        public static UploadFile GetFileById(string id)
        {
            var rep = _UploadFileService.QueryUploadFileList(new QueryUploadFileRequest
          {
              UploadFileId = id
          });
            if (rep.Result.IsSuccess)
            {
                var file = rep.UploadFileList.FirstOrDefault();
                return file;
            }
            return null;
        }
    }
}
